package com.bj.zspace.flink.study.wc.mysource;

import org.apache.flink.api.common.RuntimeExecutionMode;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

/**
 *
 * 使用: nc -lk 7777 命令开启一个服务器
 * 使用: StreamExecutionEnvironment.socketTextStream: 去监听这个服务的数据
 *
 *
 * @author qing
 */
public class UnboundStreamWordCount {

    public static void main(String[] args) throws Exception {

        // 1.创建流式执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setRuntimeMode(RuntimeExecutionMode.AUTOMATIC);
        // 2。读取文本流
        DataStreamSource<String> lineDataStreamSource = env.addSource(new VideoOrderSource());

        //3. 转换计算
        SingleOutputStreamOperator<Tuple2<String, Long>> wordAndOneTuple =
            lineDataStreamSource.flatMap((String line, Collector<Tuple2<String, Long>> out) -> {
                String[] words = line.split(" ");
                for (String word : words) {
                    out.collect(Tuple2.of(word, 1L));
                }
            }).returns(Types.TUPLE(Types.STRING, Types.LONG));

        //4.分组
        KeyedStream<Tuple2<String, Long>, String> wordAndOneKeyedStream = wordAndOneTuple.keyBy(data -> data.f0);

        // 5.求和
        SingleOutputStreamOperator<Tuple2<String, Long>> sum = wordAndOneKeyedStream.sum(1);

        // 6.打印输出
        sum.print();

        // 7.启动执行
        env.execute();



    }

}
